/*
 * @Author: cyq
 * @Date: 2023-06-30 19:13:53
 * @Last Modified by: cyq
 * @Last Modified time: 2023-07-03 20:10:59
 */

import { request } from "@/utils/request";
import { API_USER_CONFIG } from "@/config/api/user";
import { UserManageType } from "@/interfase/module/user";
import { AxiosError } from "axios";

interface useUserFace {
  login: (params: UserManageType.UserLoginFrom) => Promise<any>;
  registry: (params: UserManageType.UserRegis) => Promise<any>;
}

export const userServe = (): useUserFace => {
  class UserS {
    // 登录
    login(params: UserManageType.UserLoginFrom) {
      const url = API_USER_CONFIG.login();
      return request
        .post(url, params)
        .then(({ data }: any) => {
          return Promise.resolve(data);
        })
        .catch((error: AxiosError) => {
          return Promise.reject(error);
        });
    }

    // 注册
    registry(params: UserManageType.UserRegis) {
      const url = API_USER_CONFIG.registry();
      return request
        .post(url, params)
        .then(({ data }: any) => {
          return Promise.resolve(data);
        })
        .catch((err: AxiosError) => {
          return Promise.reject(err);
        });
    }
  }
  return new UserS();
};
